package rootdb

import "gitee.com/xiaoyutab/xgotool/optional/xcache"

// 获取设备对应的MAC地址
//
//	did	设备ID
func DerviceMacs(did int64) []DerviceMac {
	if did <= 0 {
		return nil
	}
	cache_key := xcache.Key("service.rootdb.dervice.macs", did)
	mac, macs := DerviceMac{}, []DerviceMac{}
	if xcache.Exists(cache_key) {
		if err := xcache.GetStruct(cache_key, &macs); err == nil {
			return macs
		}
		macs = []DerviceMac{}
	}
	conn, err := Connect()
	if err != nil {
		return nil
	}
	err = conn.Table(mac.TableName()).Order("id DESC").Where("did", did).Find(&macs).Error
	if err != nil {
		return nil
	}
	xcache.SetStruct(cache_key, macs)
	return macs
}
